package com.hopu.haogoumall.db.mapper;

import com.hopu.haogoumall.db.dto.CategoryGoodsDto;
import com.hopu.haogoumall.db.module.HaogoumallGoods;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface HaogoumallGoodsMapper {
    @Delete({
        "delete from haogoumall_goods",
        "where goods_id = #{goodsId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer goodsId);

    @Insert({
        "insert into haogoumall_goods (goods_id, goods_sn, ",
        "goods_name, category_id, ",
        "brand_id, goods_gallery, ",
        "goods_keywords, goods_brief, ",
        "goods_is_on_sale, goods_sort_order, ",
        "goods_pic_url, goods_share_url, ",
        "goods_is_new, goods_is_hot, ",
        "goods_unit, goods_counter_price, ",
        "goods_retail_price, goods_add_time, ",
        "goods_update_time, goods_deleted, ",
        "goods_detail)",
        "values (#{goodsId,jdbcType=INTEGER}, #{goodsSn,jdbcType=VARCHAR}, ",
        "#{goodsName,jdbcType=VARCHAR}, #{categoryId,jdbcType=INTEGER}, ",
        "#{brandId,jdbcType=INTEGER}, #{goodsGallery,jdbcType=VARCHAR}, ",
        "#{goodsKeywords,jdbcType=VARCHAR}, #{goodsBrief,jdbcType=VARCHAR}, ",
        "#{goodsIsOnSale,jdbcType=BIT}, #{goodsSortOrder,jdbcType=SMALLINT}, ",
        "#{goodsPicUrl,jdbcType=VARCHAR}, #{goodsShareUrl,jdbcType=VARCHAR}, ",
        "#{goodsIsNew,jdbcType=BIT}, #{goodsIsHot,jdbcType=BIT}, ",
        "#{goodsUnit,jdbcType=VARCHAR}, #{goodsCounterPrice,jdbcType=DECIMAL}, ",
        "#{goodsRetailPrice,jdbcType=DECIMAL}, #{goodsAddTime,jdbcType=TIMESTAMP}, ",
        "#{goodsUpdateTime,jdbcType=TIMESTAMP}, #{goodsDeleted,jdbcType=BIT}, ",
        "#{goodsDetail,jdbcType=LONGVARCHAR})"
    })
    int insert(HaogoumallGoods record);

    @InsertProvider(type=HaogoumallGoodsSqlProvider.class, method="insertSelective")
    int insertSelective(HaogoumallGoods record);

    @Select({
        "select",
        "goods_id, goods_sn, goods_name, category_id, brand_id, goods_gallery, goods_keywords, ",
        "goods_brief, goods_is_on_sale, goods_sort_order, goods_pic_url, goods_share_url, ",
        "goods_is_new, goods_is_hot, goods_unit, goods_counter_price, goods_retail_price, ",
        "goods_add_time, goods_update_time, goods_deleted, goods_detail",
        "from haogoumall_goods",
        "where goods_id = #{goodsId,jdbcType=INTEGER}"
    })

    /**@Results注解
     *      属性：
     *          id作为当前@Results注解的唯一标识很好理解；
     *          value值为@Result数组(其中可以定义实体类属性与数据库字段对应的参数)
     */
    @Results( id = "goodsResult",value = {
        @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sn", property="goodsSn", jdbcType=JdbcType.VARCHAR),
        @Result(column="goods_name", property="goodsName", jdbcType=JdbcType.VARCHAR),
        @Result(column="category_id", property="categoryId", jdbcType=JdbcType.INTEGER),
        @Result(column="brand_id", property="brandId", jdbcType=JdbcType.INTEGER),
        @Result(column="goods_gallery", property="goodsGallery", jdbcType=JdbcType.VARCHAR),
        @Result(column="goods_keywords", property="goodsKeywords", jdbcType=JdbcType.VARCHAR),
        @Result(column="goods_brief", property="goodsBrief", jdbcType=JdbcType.VARCHAR),
        @Result(column="goods_is_on_sale", property="goodsIsOnSale", jdbcType=JdbcType.BIT),
        @Result(column="goods_sort_order", property="goodsSortOrder", jdbcType=JdbcType.SMALLINT),
        @Result(column="goods_pic_url", property="goodsPicUrl", jdbcType=JdbcType.VARCHAR),
        @Result(column="goods_share_url", property="goodsShareUrl", jdbcType=JdbcType.VARCHAR),
        @Result(column="goods_is_new", property="goodsIsNew", jdbcType=JdbcType.BIT),
        @Result(column="goods_is_hot", property="goodsIsHot", jdbcType=JdbcType.BIT),
        @Result(column="goods_unit", property="goodsUnit", jdbcType=JdbcType.VARCHAR),
        @Result(column="goods_counter_price", property="goodsCounterPrice", jdbcType=JdbcType.DECIMAL),
        @Result(column="goods_retail_price", property="goodsRetailPrice", jdbcType=JdbcType.DECIMAL),
        @Result(column="goods_add_time", property="goodsAddTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="goods_update_time", property="goodsUpdateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="goods_deleted", property="goodsDeleted", jdbcType=JdbcType.BIT),
        @Result(column="goods_detail", property="goodsDetail", jdbcType=JdbcType.LONGVARCHAR)
    })

    HaogoumallGoods selectByPrimaryKey(Integer goodsId);

    @UpdateProvider(type=HaogoumallGoodsSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(HaogoumallGoods record);

    @Update({
        "update haogoumall_goods",
        "set goods_sn = #{goodsSn,jdbcType=VARCHAR},",
          "goods_name = #{goodsName,jdbcType=VARCHAR},",
          "category_id = #{categoryId,jdbcType=INTEGER},",
          "brand_id = #{brandId,jdbcType=INTEGER},",
          "goods_gallery = #{goodsGallery,jdbcType=VARCHAR},",
          "goods_keywords = #{goodsKeywords,jdbcType=VARCHAR},",
          "goods_brief = #{goodsBrief,jdbcType=VARCHAR},",
          "goods_is_on_sale = #{goodsIsOnSale,jdbcType=BIT},",
          "goods_sort_order = #{goodsSortOrder,jdbcType=SMALLINT},",
          "goods_pic_url = #{goodsPicUrl,jdbcType=VARCHAR},",
          "goods_share_url = #{goodsShareUrl,jdbcType=VARCHAR},",
          "goods_is_new = #{goodsIsNew,jdbcType=BIT},",
          "goods_is_hot = #{goodsIsHot,jdbcType=BIT},",
          "goods_unit = #{goodsUnit,jdbcType=VARCHAR},",
          "goods_counter_price = #{goodsCounterPrice,jdbcType=DECIMAL},",
          "goods_retail_price = #{goodsRetailPrice,jdbcType=DECIMAL},",
          "goods_add_time = #{goodsAddTime,jdbcType=TIMESTAMP},",
          "goods_update_time = #{goodsUpdateTime,jdbcType=TIMESTAMP},",
          "goods_deleted = #{goodsDeleted,jdbcType=BIT},",
          "goods_detail = #{goodsDetail,jdbcType=LONGVARCHAR}",
        "where goods_id = #{goodsId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKeyWithBLOBs(HaogoumallGoods record);

    @Update({
        "update haogoumall_goods",
        "set goods_sn = #{goodsSn,jdbcType=VARCHAR},",
          "goods_name = #{goodsName,jdbcType=VARCHAR},",
          "category_id = #{categoryId,jdbcType=INTEGER},",
          "brand_id = #{brandId,jdbcType=INTEGER},",
          "goods_gallery = #{goodsGallery,jdbcType=VARCHAR},",
          "goods_keywords = #{goodsKeywords,jdbcType=VARCHAR},",
          "goods_brief = #{goodsBrief,jdbcType=VARCHAR},",
          "goods_is_on_sale = #{goodsIsOnSale,jdbcType=BIT},",
          "goods_sort_order = #{goodsSortOrder,jdbcType=SMALLINT},",
          "goods_pic_url = #{goodsPicUrl,jdbcType=VARCHAR},",
          "goods_share_url = #{goodsShareUrl,jdbcType=VARCHAR},",
          "goods_is_new = #{goodsIsNew,jdbcType=BIT},",
          "goods_is_hot = #{goodsIsHot,jdbcType=BIT},",
          "goods_unit = #{goodsUnit,jdbcType=VARCHAR},",
          "goods_counter_price = #{goodsCounterPrice,jdbcType=DECIMAL},",
          "goods_retail_price = #{goodsRetailPrice,jdbcType=DECIMAL},",
          "goods_add_time = #{goodsAddTime,jdbcType=TIMESTAMP},",
          "goods_update_time = #{goodsUpdateTime,jdbcType=TIMESTAMP},",
          "goods_deleted = #{goodsDeleted,jdbcType=BIT}",
        "where goods_id = #{goodsId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(HaogoumallGoods record);


    /** 查询所有商品信息的总数量*/
    @Select("select count(goods_name) from haogoumall_goods")
    int findGoodsCount();

    /** @ResultMap中只有一个属性value(String类型的值)，用于指定要引用的@Results，value值为@Results的id值*/
    @ResultMap("goodsResult")
    @Select("select * from haogoumall_goods")
    /**查询所有商品信息，返回一个goods实体类的List集合*/
    List<HaogoumallGoods> findAllGoods();

    /** 查询所有新品发售商品信息*/
    @Select("select * from haogoumall_goods where goods_is_new = 1")
    List<HaogoumallGoods> findNewGoodsInfo();

    /** 查询所有人气商品*/
    @Select("select * from haogoumall_goods where goods_is_hot = 1")
    List<HaogoumallGoods> findHotGoodsInfo();

    /** 根据分类名称查询对应的商品信息*/
    @Select("select * from haogoumall_goods A,haogoumall_category B where A.category_id = B.category_id and B.category_pid=(select category_id from haogoumall_category where category_name = #{categoryName})")
    List<CategoryGoodsDto> findFloorGoodInfo(String categoryName);
}